"""

Computational Economics
01: First Steps
http://johnstachurski.net/lectures/firststeps.html

"""


"""
    Problem 1:

    * Start with Xints = [1, 2, 3, 4]
          o In other words, start by typing Xints = [1, 2, 3, 4] at the prompt
    * Convert to floats: Xfloats = [1.0, 2.0, 3.0, 4.0]
          o That is, type Xfloats = [put your code in here]
"""
Xints = [1, 2, 3, 4]
Xfloats = [float(n) for n in Xints]
assert Xfloats == [1.0, 2.0, 3.0, 4.0]


"""
    Problem 2:

    * Start with the string S = '1234'
    * Convert into the list of integers X = [1, 2, 3, 4]
"""
S = '1234'
X = [int(c) for c in S]
assert X == [1, 2, 3, 4]


"""
    Problem 3:

    * Start with L = ['foo', 'bar', 'spam', 'eggs']
    * Extract first letter of each word: F = ['f', 'b', 's', 'e']
"""
L = ['foo', 'bar', 'spam', 'eggs']
F = [w[0] for w in L]
assert F == ['f', 'b', 's', 'e']


"""
    Problem 4:

    * Start with filenames = ['file1.txt', 'file2.txt', 'file3.txt']
    * Convert into newnames = ['file1.html', 'file2.html', 'file3.html']
    * Hint: use replace()
"""
filenames = ['file1.txt', 'file2.txt', 'file3.txt']
newnames = [f.replace('txt', 'html') for f in filenames]
assert newnames == ['file1.html', 'file2.html', 'file3.html']


"""
    Problem 5:

    * Start with filenames = ['file1.txt', 'file2.html', 'file3.doc']
    * Convert into newnames = ['file1', 'file2', 'file3']
    * Hint: use split()
"""
filenames = ['file1.txt', 'file2.html', 'file3.doc']
newnames = [f.split('.')[0] for f in filenames]
newnames == ['file1', 'file2', 'file3']


"""
    Problem 6:

    * Start with singular = ['apple', 'orange', 'banana']
    * Convert to plural = ['apples', 'oranges', 'bananas']
"""
singular = ['apple', 'orange', 'banana']
plural = ['%ss' % (w) for w in singular]
assert plural == ['apples', 'oranges', 'bananas']


"""
    Problem 7:

    * Start with X = [(3, 4), (9, -1), (8, 11)]
    * Convert to list of sums: Y = [7, 8, 19]
          o Y[0] = 3 + 4, Y[1] = 9 - 1, etc.
"""
X = [(3, 4), (9, -1), (8, 11)]
Y = [t[0] + t[1] for t in X]
assert Y == [7, 8, 19]


print '%s: ok' % (__file__)
